<?php
namespace other\orders;
use other\ShopingConn;
use other\Table;

/**
 * 订单状态修改
 */
class StatusEx
{
    /**
     * 待修改的订单
     * @var array
     */
    private $payment_ids = [];

    private $order_ids = [10458,10538];

    /**
     * 处理    Abnormal
     * @return void
     * @throws \think\Exception
     */
    public function do()
    {
        $this->ConvertToPaid();
        exit();
    }


    /**
     * 根据订单ID 将订单调整为 正常收款的
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    private function ConvertToPaid()
    {
        $this->_doIds();
        exit();

        $this->payment_ids = [
            '2UV65453ME569102U', '6K71380388416823H', '66739248070691052'
        ];

        if ($this->payment_ids) {
            $collection = ShopingConn::getConn()->table(Table::Table_Order)
                ->field('id,status,pay_time,created_at,error_msg,order_type,is_import,ship_order_id')
                ->where('payment_id', 'in', $this->payment_ids)
                ->select();

            foreach ($collection as $row) {

                $id = $row['id'];
                $status = 1;    // 成功支付的订单
                $pay_time = $row['created_at'];
                $order_type = 1;
                $error_msg = '';
                $order_sn = '#' . ShopingConn::getRandOnlyId();

                ShopingConn::getConn()->table(Table::Table_Order)
                    ->where('id', $id)
                    ->update(compact("status", "pay_time", "order_type", "error_msg", "order_sn"));
            }
        }
    }


    private function _doIds(){
        if ($this->order_ids) {
            $collection = ShopingConn::getConn()->table(Table::Table_Order)
                ->field('id,status,pay_time,created_at,error_msg,order_type,is_import,ship_order_id')
                ->where('id', 'in', $this->order_ids)
                ->select();

            foreach ($collection as $row) {

                $id = $row['id'];
                $status = 1;    // 成功支付的订单
                $pay_time = $row['created_at'];
                $order_type = 1;
                $error_msg = '';
                $order_sn = '#' . ShopingConn::getRandOnlyId();

                ShopingConn::getConn()->table(Table::Table_Order)
                    ->where('id', $id)
                    ->update(compact("status", "pay_time", "order_type", "error_msg", "order_sn"));
            }
        }
    }

}
